/*******************************************************************************
 * 占쎌쥙猷욑옙�용쐻占쎈갭梨울옙占� PagingUtil.java
 * 占쎌쥙�쀯옙�뚯굲占쎌쥙猷욑옙占� 占쎌쥙猷욑옙�낅눀�좑옙 * 占쎌쥙�쀯옙�뚯굲占쎌쥙猷욑옙�용쐻占쎌늿��: 2013. 01. 01
 *
 * 占쎈떣�앾옙�덉굲占쎌쥙猷욑옙占쏙옙醫롫짗占쎌눨�앾옙�덉굲: 占쎌쥙猷욑옙�용쐻占쎌늿�뺧쭪占� * =============================================================================
 * 占쎌쥙猷욑옙�용쐻占쎌늿�뺧옙醫롫짗占쎌눨�앾옙�덉굲
 * NO   占쎌쥙猷욑옙�용쐻占쎌늿�뺧옙醫롫짗占쎌눨�앾옙�덉굲			占쎌쥙猷욑옙�용쐻占쎌늿�뺧옙醫롫짗占쏙옙	占쎌쥙猷욑옙�용쐻占쎌늿�뺧옙醫롫짗占쎌눨�앾옙�덉굲  
 * 001. 2013. 01. 01	占쎌쥙猷욑옙�낅눀�좑옙		占쎌쥙猷욑옙�용쐻占쎌늿�뺧옙醫롫솙占쎌럩��
 * 002. 2013. 05. 15    占쎌쥙猷욑옙�낅눀�좑옙             占쎌쥙�먲옙��굲占쎌쥙猷욑옙占쏙옙醫롫짗占쎌눨�앾옙�롮굻占쏙옙筌ｌ꼪�앾옙�덉굲占쎌쥙猷욑옙占쏙옙醫롫짗占쏙옙占쎌쥙�뉛옙紐꾩굲占쎌쥙猷욑옙占쏙옙醫롫짗占쎌눨�앾옙�덉굲 占쎌쥙�ζ��쇱굲
 * 003. 2014. 01. 02    占쎌쥙猷욑옙�낅눀�좑옙             占쎌쥙�숋옙�깆굲占쎌쥙猷욑옙占퐑rl 占쎌쥙猷욑옙占쏙옙醫롫짗占쎌눨�앾옙�덉굲 占쎌쥙猷욑옙�용쐻占쎌늿��
 * =============================================================================
 */
package some.talk.util;

public class PagingUtil {
	private int startCount;	 // 占쎌쥙猷욑옙占쏙옙醫롫짗占쎌눨�앾옙�덉굲占쎌쥙猷욑옙�용쐻占쎌늿�뺧옙醫롫짗占쏙옙占쎌쥙猷욑옙�용쐻占쎌늿�뺧옙醫롫짗占쏙옙占쎌쥙�껓옙�볧돲占쎌눨�앾옙�덉굲 占쎌쥙猷욑옙�용쐻占쎌늿��占쎌쥙猷욑옙�됱깈
	private int endCount;	 // 占쎌쥙猷욑옙占쏙옙醫롫짗占쎌눨�앾옙�덉굲占쎌쥙猷욑옙�용쐻占쎌늿�뺧옙醫롫짗占쏙옙占쎌쥙猷욑옙�용쐻占쎌늿�뺧옙醫롫짗占쏙옙占쎌쥙�껓옙�볧돲占쎌눨�앾옙�덉굲 占쎌쥙猷욑옙占쏙옙醫롫짗占쎌닀��
	private StringBuffer pagingHtml;// 占쎌쥙猷욑옙�용쐻占쎌늿�뺧쭪占쏙옙醫롫짗占쎌눨�앾옙�덉굲
  
	/**
	 * currentPage : 占쎌쥙猷욑옙�용쐻占쎌늿�뺧옙醫롫짗占쎌눨�앾옙�덉굲占쎌쥙猷욑옙占�	 * totalCount : 占쎌쥙猷욑옙�덇퍥 占쎌쥙�껓옙�뺤퐟占쏙옙占쎌쥙猷욑옙占�	 * blockCount : 占쎌쥙猷욑옙占쏙옙醫롫짗占쎌눨�앾옙�덉굲占쎌쥙猷욑옙�용쐻占쎌늿�� 占쎌쥙�껓옙�뺤퐟占쎌눨�앾옙�덉굲 占쎌쥙猷욑옙占�	 * blockPage : 占쎌쥙猷욑옙占쏙옙遺룸쐻占쎌럩肉�占쎌쥙猷욑옙�용쐻占쎌늿�뺧옙醫롫짗占쏙옙占쎌쥙猷욑옙�용쐻占쎌늿�뺧옙醫롫짗占쏙옙占쎌쥙猷욑옙占�	 * pageUrl : 占쎈ŀ�앾옙�덉굲 占쎌쥙猷욑옙�용쐻占쎌늿�뺧옙醫롫짗占쏙옙url
	 * addKey : 占쎌쥙�ζ��쇱굲占쎌쥙猷욑옙�용쐻占쎌늿��key 占쎌쥙猷욑옙�용쐻占쎌늿��占쎌쥙猷욑옙�용쐻占쎌늿��null 筌ｌ꼪�앾옙�덉굲 (&num=23占쎌쥙猷욑옙�용쐻占쎌늿�뺧옙醫롫짗占쎌눨�앭뜝�뀀쐻占쎌늿�뺧옙醫롫짗占쎌눨�앭뜝�뀀쐻占쎌늿��
	 * */
	public PagingUtil(int currentPage, int totalCount, int blockCount,
			int blockPage, String pageUrl) {
		this(null,null,currentPage,totalCount,blockCount,blockPage,pageUrl,null);
	}
	public PagingUtil(int currentPage, int totalCount, int blockCount,
			int blockPage, String pageUrl, String addKey) {
		this(null,null,currentPage,totalCount,blockCount,blockPage,pageUrl,addKey);
	}
	public PagingUtil(String keyField, String keyWord, int currentPage, int totalCount, int blockCount,
			int blockPage,String pageUrl) {
		this(keyField,keyWord,currentPage,totalCount,blockCount,blockPage,pageUrl,null);
	}
	public PagingUtil(String keyField, String keyWord, int currentPage, int totalCount, int blockCount,
			int blockPage,String pageUrl,String addKey) {
		
		if(addKey == null) addKey = ""; //占쎌쥙�ζ��쇱굲占썬뀿�앾옙�덉굲 null 占쎌쥙��옙怨쀬굲 ""筌ｌ꼪�앾옙�덉굲
		
		// 占쎌쥙猷욑옙�덇퍥 占쎌쥙猷욑옙�용쐻占쎌늿�뺧옙醫롫짗占쏙옙占쎌쥙猷욑옙占�		int totalPage = (int) Math.ceil((double) totalCount / blockCount);
		int totalPage = (int) Math.ceil((double) totalCount / blockCount);
		if (totalPage == 0) {
			totalPage = 1;
		}
		// 占쎌쥙猷욑옙�용쐻占쎌늿��占쎌쥙猷욑옙�용쐻占쎌늿�뺧옙醫롫짗占쎌눨�앾옙�덉굲 占쎌쥙猷욑옙�덇퍥 占쎌쥙猷욑옙�용쐻占쎌늿�뺧옙醫롫짗占쏙옙占쎌쥙猷욑옙�용쐻占쎌늿�뺧옙醫롫짗占쏙옙占싼껊쐻占쎌늿��占쎌쥙猷욑옙�덇퍥 占쎌쥙猷욑옙�용쐻占쎌늿�뺧옙醫롫짗占쏙옙占쎌쥙猷욑옙�용쐻占쎌늿��占쎌쥙猷욑옙�용쐻占쎌늿��
		if (currentPage > totalPage) {
			currentPage = totalPage;
		}
		// 占쎌쥙猷욑옙�용쐻占쎌늿��占쎌쥙猷욑옙�용쐻占쎌늿�뺧옙醫롫짗占쎌눨�앾옙�덉굲 筌ｌ꼪�앾옙�덉굲占쎌쥙猷욑옙占쏙옙醫롫짗占쎌눨�앾옙�덉굲占쎌쥙猷욑옙占쏙옙醫롫짗占쎌눨�앾옙�덉굲 占쎌쥙猷욑옙�됱깈 占쎌쥙猷욑옙�용쐻占쎌늿�뺧옙醫롫짗占쎌눨�앾옙�덉굲.
		startCount = (currentPage - 1) * blockCount + 1;
		endCount = currentPage * blockCount;
		// 占쎌쥙猷욑옙�용쐻占쎌늿��占쎌쥙猷욑옙�용쐻占쎌늿�뺧옙醫롫짗占쎌눨�앾옙�덉굲 占쎌쥙猷욑옙�용쐻占쎌늿�뺧옙醫롫짗占쏙옙占쎌쥙猷욑옙�용쐻占쎌늿�뺧옙醫롫짗占쏙옙占쎌쥙猷욑옙占쏙옙醫롫짗占쎌눨�앾옙諛댄돲占쏙옙
		int startPage = (int) ((currentPage - 1) / blockPage) * blockPage + 1;
		int endPage = startPage + blockPage - 1;
		// 占쎌쥙猷욑옙�용쐻占쎌늿�뺧옙醫롫짗占쏙옙占쎌쥙猷욑옙�용쐻占쎌늿�뺧옙醫롫짗占쎌눨�앾옙�덉굲 占쎌쥙猷욑옙�덇퍥 占쎌쥙猷욑옙�용쐻占쎌늿�뺧옙醫롫짗占쏙옙占쎌쥙猷욑옙�용쐻占쎌늿�뺧옙醫롫짗占쏙옙占싼껊쐻占쎌늿��占쎌쥙猷욑옙�덇퍥 占쎌쥙猷욑옙�용쐻占쎌늿�뺧옙醫롫짗占쏙옙占쎌쥙猷욑옙�용쐻占쎌늿��占쎌쥙猷욑옙�용쐻占쎌늿��
		if (endPage > totalPage) {
			endPage = totalPage;
		}
		// 占쎈똻�얕린袁る뱣甕곌쑵��
		pagingHtml = new StringBuffer();
		if (currentPage > blockPage) {
			if(keyWord==null){//野껓옙源뗰옙�울옙 占쎈뜄��野껋럩��
				pagingHtml.append("<span id='pagingPrev'><a href="+pageUrl+"?pageNum="+ (startPage - 1) + addKey +">");
			}else{
				pagingHtml.append("<span id='pagingPrev'><a href="+pageUrl+"?keyField="+keyField+"&keyWord="+keyWord+"&pageNum="+ (startPage - 1) + addKey +">");
			}
			pagingHtml.append("prev");
			pagingHtml.append("</a></span>");
		}
		pagingHtml.append("&nbsp;|&nbsp;");
		//占쎌꼷�좑쭪占쏙㎗�롡봺
		for (int i = startPage; i <= endPage; i++) {
			if (i > totalPage) {
				break;
			}
			if (i == currentPage) {
				pagingHtml.append("&nbsp;<span id='pagingNowSet'><b> <font color='red'>");
				pagingHtml.append(i);
				pagingHtml.append("</font></b></span>");
			} else {
				if(keyWord==null){//占쎌쥚源�옙占쏙옙�륁뵠筌욑옙占�占쎄쑬��占쎌꼷�좑쭪占�
					pagingHtml.append("&nbsp;<span id='pagingOtherSet'><a href='"+pageUrl+"?pageNum=");
				}else{
					pagingHtml.append("&nbsp;<span id='pagingOtherSet'><a href='"+pageUrl+"?keyField="+keyField+"&keyWord="+keyWord+"&pageNum=");
				}
				pagingHtml.append(i);
				pagingHtml.append(addKey+"'>");
				pagingHtml.append(i);
				pagingHtml.append("</a></span>");
			}
			pagingHtml.append("&nbsp;");
		}
		pagingHtml.append("&nbsp;&nbsp;|&nbsp;&nbsp;");
		// 占썬끉�ц린袁る뱣
		if (totalPage - startPage >= blockPage) {
			if(keyWord==null){//野껓옙源뗰옙�울옙 占쎈뜆�ｅ칰�뚯뒭
				pagingHtml.append("<span id='pagingNext'><a href="+pageUrl+"?pageNum="+ (endPage + 1) + addKey +">");
			}else{
				pagingHtml.append("<span id='pagingNext'><a href="+pageUrl+"?keyField="+keyField+"&keyWord="+keyWord+"&pageNum="+ (endPage + 1) + addKey +">");
			}
			pagingHtml.append("next");
			pagingHtml.append("</a></span>");
		}
	}
	public StringBuffer getPagingHtml() {
		return pagingHtml;
	}
	public int getStartCount() {
		return startCount;
	}
	public int getEndCount() {
		return endCount;
	}

}
